package com.jinandi.app.service;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.encoding.Md5PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jinandi.app.dao.AuthoritAndResDao;
import com.jinandi.app.dao.AuthorityDao;
import com.jinandi.app.dao.OrganizationDao;
import com.jinandi.app.dao.ResourceDao;
import com.jinandi.app.dao.RoleDao;
import com.jinandi.app.dao.UserDao;
import com.jinandi.app.domain.Authorit;
import com.jinandi.app.domain.AuthoritAndRes;
import com.jinandi.app.domain.Organization;
import com.jinandi.app.domain.Resource;
import com.jinandi.app.domain.Role;
import com.jinandi.app.domain.User;

@Service
public class InitService {

	@Autowired
	private UserDao userDao;//
	@Autowired
	private RoleDao roleDao;//
	@Autowired
	private AuthorityDao authorityDao;//
	@Autowired
	private ResourceDao resourceDao;
	@Autowired
	private AuthoritAndResDao andResDao;
	
	@Autowired
	private OrganizationDao organizationDao;

	@Transactional
	public void init() {
		Md5PasswordEncoder encoder = new Md5PasswordEncoder();
		
		Authorit authority = new Authorit();
		authority.setAuName("管理员");
		authority.setAuthority("ROLE_ADMIN");
		authorityDao.save(authority);
		
		Authorit authority1 = new Authorit();
		authority1.setAuName("普通用户");
		authority1.setAuthority("ROLE_USER");
		authorityDao.save(authority1);
		
		User user = new User();
		user.setName("管理员");
		user.setUsername("admin");
		user.setPassword(encoder.encodePassword("1","admin"));
		userDao.save(user);
		
		User user1 = new User();
		user1.setName("普通用户");
		user1.setUsername("user");
		user1.setPassword(encoder.encodePassword("1","user"));
		userDao.save(user1);
		
		Role role = new Role();
		role.setAuthorit(authority);
		role.setUser(user);
		roleDao.save(role);
		
		Role role1 = new Role();
		role1.setAuthorit(authority1);
		role1.setUser(user1);
		roleDao.save(role1);
		
		Resource resource = new Resource();
		resource.setUrl("/admin/*");
		resourceDao.save(resource);
		
		Resource resource2 = new Resource();
		resource2.setUrl("/main/*");
		resourceDao.save(resource2);
		
	}
	
	@Transactional
	public void init2() {
		AuthoritAndRes andRes = new AuthoritAndRes();
		andRes.setAuthorit(authorityDao.findAuthoritById(new Long(1)));
		andRes.setResource(resourceDao.findResById(new Long(1)));
		andResDao.save(andRes);
		
		AuthoritAndRes andRes2 = new AuthoritAndRes();
		andRes2.setAuthorit(authorityDao.findAuthoritById(new Long(1)));
		andRes2.setResource(resourceDao.findResById(new Long(2)));
		andResDao.save(andRes2);
	}
	
	@Transactional
	public void init3() {
		Organization organization = new Organization();
		organization.setCn("shandong");
		organization.setName("山东");
		organization.setPath("/root/shandong");
		organization.setPid(new Long(1));
		organizationDao.save(organization);
		
		Organization organization1 = new Organization();
		organization1.setCn("jinan");
		organization1.setName("济南");
		organization1.setPath("/root/shandong/jinan");
		organization1.setPid(new Long(2));
		organizationDao.save(organization1);
		
		
	}
	
}
